import { Message, MessageBox } from 'element-ui'

// 成功：顶部短消息提示
function success(message = '操作成功') {
  return Message({
    type: 'success',
    message,
    showClose: true
  })
}

// 警告：顶部短消息提示
function warn(message = '操作失败') {
  return Message({
    type: 'error',
    message,
    showClose: true
  })
}

// 失败：提示错误语
const error = (function() {
  let isCurrentAlert = false
  return function(message = '网络错误', callback) {
    if (isCurrentAlert) {
      console.error(`[Alert block]: ${message}`)
      return
    }
    isCurrentAlert = true
    return MessageBox.alert(message, '错误', {
      customClass: 'f-messagebox',
      dangerouslyUseHTMLString: true,
      callback() {
        isCurrentAlert = false
        if (callback) {
          callback()
        }
      }
    })
  }
})()

// 自定义Alert
function alert(message, title = '提示', opt = {}) {
  opt.customClass = (opt.customClass || '') + ' f-messagebox'
  return MessageBox.alert(message, title, opt)
}

// 自定义Confirm
function confirm(message, title = '提示', opt = {}) {
  opt.customClass = (opt.customClass || '') + ' f-messagebox'
  opt.cancelButtonClass = (opt.cancelButtonClass || '') + ' is-plain'
  if (opt.isDanger) {
    opt.confirmButtonClass =
      (opt.confirmButtonClass || '') + 'el-button--danger'
  }
  return MessageBox.confirm(message, title, opt)
}

// 自定义prompt
function prompt(message, title = '提示', opt = {}) {
  opt.customClass = (opt.customClass || '') + ' f-messagebox'
  opt.cancelButtonClass = (opt.cancelButtonClass || '') + ' is-plain'
  if (opt.isDanger) {
    opt.confirmButtonClass =
      (opt.confirmButtonClass || '') + 'el-button--danger'
  }
  return MessageBox.prompt(message, title, opt)
}

export default {
  success,
  warn,
  error,
  alert,
  confirm,
  prompt
}
